﻿using XQ.RequestOfficeSupplies.DAL.DAL;
using XQ.RequestOfficeSupplies.DAL.Model;
using System.Collections.Generic;
using XQ.RequestOfficeSupplies.DAL.Model.Business;

namespace XQ.RequestOfficeSupplies.BLL.home
{

    /// <summary>
    /// index页面业务操作
    /// </summary>
    /// <author>毛志峰</author>
    public class IndexBll
    {
        IndexDal indexDal = null;//数据层控制类

        /// <summary>
        /// 获取用户所有的菜单
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="depId"></param>
        /// <returns></returns>
        public MenuModel GetMenusByUser(int userId, int depId)
        {
            indexDal = new IndexDal();
            List<SysMenusExModel> menus = indexDal.GetParentMenus(userId);
            var result = new MenuModel { pId = -1, title = "" };
            var subItem = new List<MenuModel>();
            result.children = subItem;
            AddChild(result, menus, userId, depId);
            return result;
        }

        /// <summary>
        /// 递归添加
        /// </summary>
        /// <param name="node"></param>
        /// <param name="menus"></param>
        /// <param name="userId"></param>
        /// <param name="depId"></param>
        private void AddChild(MenuModel node, List<SysMenusExModel> menus, int userId, int depId)
        {
            indexDal = new IndexDal();
            List<SysMenusExModel> newMenus;
            bool open = false;
            if (node.pId == -1)
            {
                newMenus = menus;
                open = true;
            }
            else { newMenus = indexDal.GetSubItem(node.id, userId); }
            if (newMenus != null && newMenus.Count > 0)
            {
                foreach (SysMenusExModel _node in newMenus) //再次遍历，添加子节点
                {
                    //int caseNumber = cb.GetCaseNumByWhere(
                    //        new CaseSearchWhere()
                    //        {
                    //            CurFlowNodeId = _node.FlowNodeId == null ? "0" : _node.FlowNodeId.ToString()
                    //        }, depId);
                    //var subItem = new List<MenuModel>();
                    var subNode = new MenuModel();
                    subNode.id = _node.ID;
                    subNode.name = _node.MenuName;// + (caseNumber==0?"":"["+caseNumber+"]");
                    subNode.title = _node.MenuName;
                    subNode.open = open;
                    //subNode.url = _node.Linkurl;
                    subNode.pId = _node.ParentID == null ? 0 : (int)_node.ParentID;
                    subNode.flowNodeId = _node.FlowNodeId;
                    subNode.children = new List<MenuModel>();
                    //subNode.HaveCaseNumber= caseNumber;
                    subNode.IsRefresh = _node.IsRefresh;
                    subNode.InvokingMothed = _node.InvokingMothed;
                    subNode.iconUrl = _node.iconUrl;
                    subNode.isVisible = _node.IsVisible;
                    subNode.path = _node.Linkurl;
                    subNode.component = _node.Component;
                    subNode.hidden = _node.IsVisible;
                    subNode.icon = _node.iconUrl;

                    node.children.Add(subNode);
                    //递归
                    AddChild(subNode, menus, userId, depId);
                }
            }

        }


    }
}
